/*
 * @Description: 全局配置
 * @Author: SkyinHand
 * @Email: zstk666@gmail.com
 * @Date: 2021-07-18 10:11:02
 * @LastEditTime: 2021-07-18 11:26:58
 * @LastEditors: SkyinHand
 */

import 'dart:io';

import 'package:device_info/device_info.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:high_help_apps/common/entities/user/user_auth_entity.dart';
import 'package:package_info/package_info.dart';

import 'common/entities/user/user_profile_response_entity.dart';
import 'common/utils/http.dart';
import 'common/utils/storage.dart';
import 'common/values/storage.dart';

class Global {
  /// 用户验证体
  static UserAuthEntity authEntity = UserAuthEntity();

  /// 用户缓存信息
  static UserProfileResponseDetail profile = UserProfileResponseDetail();

  /// 发布渠道
  static String channel = "onePlus";

  /// 是否 ios
  static bool isIOS = Platform.isIOS;

  /// android 设备信息
  static late AndroidDeviceInfo androidDeviceInfo;

  /// ios 设备信息
  static late IosDeviceInfo iosDeviceInfo;

  /// 包信息
  static late PackageInfo packageInfo;

  /// 是否第一次打开
  static bool? isFirstOpen;

  /// 是否离线登录
  static bool isOfflineLogin = false;

  /// 是否 release
  static bool get isRelease => bool.fromEnvironment("dart.vm.product");

  /// init
  static Future init() async {
    // 运行初始
    WidgetsFlutterBinding.ensureInitialized();

    // 读取设备信息
    DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    if (Global.isIOS) {
      Global.iosDeviceInfo = await deviceInfoPlugin.iosInfo;
    } else {
      Global.androidDeviceInfo = await deviceInfoPlugin.androidInfo;
    }

    // 包信息
    Global.packageInfo = await PackageInfo.fromPlatform();

    // 工具初始
    await StorageUtil().init();
    HttpUtil();

    StorageUtil().remove(STORAGE_DEVICE_FIRST_OPEN_KEY);

    // 读取设备第一次打开
    isFirstOpen = StorageUtil().getBool(STORAGE_DEVICE_FIRST_OPEN_KEY);

    if (isFirstOpen == null) {
      isFirstOpen = true;
    }

    // 读取离线用户信息
    var _profileJSON = StorageUtil().getJSON(STORAGE_USER_PROFILE_KEY);
    if (_profileJSON != null) {
      authEntity = UserAuthEntity.fromJson(_profileJSON);
      isOfflineLogin = true;
    }

    // android 状态栏为透明的沉浸
    if (Platform.isAndroid) {
      SystemUiOverlayStyle systemUiOverlayStyle =
          SystemUiOverlayStyle(statusBarColor: Colors.transparent);
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }
  }

  // 保存用户已打开APP
  static saveAlreadyOpen() {
    StorageUtil().setBool(STORAGE_DEVICE_FIRST_OPEN_KEY, false);
  }

  // 持久化 用户身份
  static Future<bool> saveAuth(UserAuthEntity userProfileEntity) {
    authEntity = userProfileEntity;
    return StorageUtil()
        .setJSON(STORAGE_USER_AUTH_KEY, userProfileEntity.toJson());
  }

  // 缓存 用户信息
  static Future<bool> saveProfile(UserProfileResponseDetail userProfileResponseDetail) {
    profile = userProfileResponseDetail;
    return StorageUtil()
        .setJSON(STORAGE_USER_PROFILE_KEY, userProfileResponseDetail.toJson());
  }

  /// 隐藏顶部状态栏
  static void hideTopStatusBar() {
    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
  }

  /// 隐藏底部状态栏
  static void hideBottomStatusBar() {
    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]);
  }

  /// 隐藏所有状态栏
  static void hideAllStatusBar() {
    SystemChrome.setEnabledSystemUIOverlays([]);
  }

  /// 显示所有状态栏
  static void showAllStatusBar() {
    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom, SystemUiOverlay.top]);
  }

}
